//
//  CALWeightViewController.m
//  CalCounter
//
//  Created by Le thanh Lam on 06/09/2013.
//  Copyright (c) 2013 Le thanh Lam. All rights reserved.
//

#import "CALWeightViewController.h"
#import "DateTimeView.h"
#import "DataHelper.h"
#import "CALDiaryInfo.h"
#import "NSString+Ex.h"
@interface CALWeightViewController ()<DateTimeViewDelegate>{
    __weak IBOutlet DateTimeView *dateBarView;
}

@end

@implementation CALWeightViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated{
    //set value for txt weight
    [self showWeightText];

    [super viewWillAppear:animated];
    [dateBarView refresh];
}

- (void)viewDidLoad
{
    [self.navigationItem setTitle:NSLocalizedString(@"weight", nil)];
    
    //add date time view

    [scrView setContentSize:CGSizeMake(320, 500)];
    //add save button on navigation
    UIBarButtonItem *btnSave = [[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStylePlain target:self action:@selector(btnSaveClicked:)];
    self.navigationItem.rightBarButtonItem = btnSave;
    txtWeight.delegate = self;
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

//check only number
//- (BOOL)validateTextOnlyNumeric:(NSString *)text{
//    BOOL valid;
//    NSCharacterSet *alphaNums = [NSCharacterSet decimalDigitCharacterSet];
//    NSCharacterSet *inStringSet = [NSCharacterSet characterSetWithCharactersInString:text];
//    valid = [alphaNums isSupersetOfSet:inStringSet];
//    return valid;
//}

- (void)btnSaveClicked:(id)sender{
    txtWeight.text = [txtWeight.text stringByReplacingOccurrencesOfString:@"," withString:@"."];
    static NSString *floatRegex = @"^(?:[1-9]\\d*|0)?(?:\\.\\d+)?$";
    BOOL isValid = [txtWeight.text isMatchRegex:floatRegex];
    if (isValid) {
        if ([self limitedSymbol:txtWeight.text]) {
            NSLog(@"input access");
            [[DataHelper sharedDatabase] addWeight:[txtWeight.text floatValue] inTime:[DateTimeView selectDate]];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
    }
    else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:NSLocalizedString(@"InvalidValue", nil) delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        NSLog(@"input not access");
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

#pragma mark - process Time delegate
- (void)whenChangeTime:(NSDate *)_date{
    [self showWeightText];
}

- (void)showWeightText{
    CALDiaryInfo *diaryInfo = [[DataHelper sharedDatabase] getDiaryInfoByDate:[DateTimeView selectDate]];
    if (diaryInfo.diaryWeight) {
        txtWeight.text =[NSString stringWithFormat:@"%1.1f",diaryInfo.diaryWeight];
    }
    else{
        txtWeight.text = @"";
    }
}

- (BOOL)limitedSymbol:(NSString *)str{
    
    if ([str floatValue] > 9999) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:NSLocalizedString(@"BigValue", nil) delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
        return NO;
    }
    else{
        return YES;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
